package com.fenghongzhang.lib_jetpack.room.raw

import android.content.ContentValues
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.room.Room
import com.fenghongzhang.lib_jetpack.R
import kotlinx.coroutines.launch

class JetpackMainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.jetpack_activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        val db = Room.databaseBuilder(
            applicationContext,
            AppDatabase::class.java, "database-name"
        )
            .addMigrations(MIGRATION_1_2)
            .build()

        val userDao = db.userDao()
        lifecycleScope.launch {
            userDao.insertAll(User(1,"1",1))
        }
    }

    private fun raw() {
        val dbHelper = MysqliteHelper(this)
        val db = dbHelper.writableDatabase

        val values = ContentValues().apply {
            put("name", "Alice")
            put("age", 25)
        }

        db.insert("users", null, values)


        val columns = arrayOf("id", "name", "age")
        val cursor = db.query("users", columns, null, null, null, null, null)

        with(cursor) {
            while (moveToNext()) {
                val id = getInt(getColumnIndexOrThrow("id"))
                val name = getString(getColumnIndexOrThrow("name"))
                val age = getInt(getColumnIndexOrThrow("age"))
                println("ID: $id, Name: $name, Age: $age")
                val user = User(id, name, age)
            }
        }

        cursor.close()
        db.close()
    }
}